import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:jm_flutter_common/jm_flutter_common.dart';
import 'package:share_plus/share_plus.dart';
import '../../../common/utils/i18n.dart';
import '../../../common/utils/shortcut.dart';
import '../../../common/utils/show.dart';
import '../../ui.dart';
import 'common/config.dart';
import 'debug_overlay.dart';

class MyDebugPage extends StatefulWidget {
  final Widget? child;
  const MyDebugPage({super.key, this.child});

  @override
  State<MyDebugPage> createState() => _DebugPageState();
}

class _DebugPageState extends State<MyDebugPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (DebugOverLay.isShow) {
      DebugOverLay.hide();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MyScaffold(
      title: "Debug",
      i18n: false,
      actions: [
        PopupMenuButton<String>(
          icon: Icon(Icons.more_vert, color: JmColors.appBarText),
          onSelected: (value) async {
            switch (value) {
              case 'file_manager':
                if (kIsWeb) {
                  logd("MyDebugPage", "web不支持文件管理");
                  Show.toast("Web不支持文件管理");
                  return;
                }
                goto(() => const AppFilePage());
                break;
              case 'debug_overlay':
                if (DebugOverLay.isShow) {
                  DebugOverLay.hide();
                } else {
                  DebugOverLay.show(context: context);
                  back();
                }
                break;
              case 'clear_log':
                JmLog.clear();
                Show.toast("日志已清除");
                break;
              case 'save_log':
                Show.toast("保存日志功能待实现");
                break;
              case 'export_settings':
                Show.toast("导出设置功能待实现");
                break;
              case 'export_i18n_text':
                final filePath = await I18n.exportTextSetRecordJsonFile();
                final xFiles = [XFile(filePath)];

                final ret = await Share.shareXFiles(xFiles);
                if (ret.status == ShareResultStatus.success) {
                  Show.toast("导出未翻译文本成功");
                } else {
                  Show.toast("导出未翻译文本失败");
                }

                break;
            }
          },
          itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
            const PopupMenuItem<String>(
              value: 'file_manager',
              child: ListTile(
                leading: Icon(CupIcons.folder_badge_person_crop),
                title: Text('文件管理'),
                contentPadding: EdgeInsets.zero,
              ),
            ),
            const PopupMenuItem<String>(
              value: 'debug_overlay',
              child: ListTile(
                leading: Icon(Icons.bug_report),
                title: Text('调试浮窗'),
                contentPadding: EdgeInsets.zero,
              ),
            ),
            const PopupMenuDivider(),
            const PopupMenuItem<String>(
              value: 'clear_log',
              child: ListTile(
                leading: Icon(Icons.clear_all),
                title: Text('清除日志'),
                contentPadding: EdgeInsets.zero,
              ),
            ),
            const PopupMenuItem<String>(
              value: 'save_log',
              child: ListTile(
                leading: Icon(Icons.save),
                title: Text('保存日志'),
                contentPadding: EdgeInsets.zero,
              ),
            ),
            const PopupMenuDivider(),
            const PopupMenuItem<String>(
              value: 'export_settings',
              child: ListTile(
                leading: Icon(Icons.settings_backup_restore),
                title: Text('导出设置'),
                contentPadding: EdgeInsets.zero,
              ),
            ),
            const PopupMenuItem<String>(
              value: 'export_i18n_text',
              child: ListTile(
                leading: Icon(Icons.settings_backup_restore),
                title: Text('导出未翻译文本'),
                contentPadding: EdgeInsets.zero,
              ),
            ),
          ],
        ),
      ],
      body: Column(
        children: [
          widget.child ?? const SizedBox(),
          Expanded(
              child: LogWidget(
                  showTime: showTime,
                  logLevel: logLevel,
                  onShowTimeChanged: (value) {
                    showTime = value;
                  },
                  onLogLevelChanged: (value) {
                    logLevel = value;
                  })),
        ],
      ),
    );
  }
}
